﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class VizualizareGrafic : UserControl
    {
        string CNP;
        string limba;
        Dictionary<int, string> senzorNume = new Dictionary<int, string>();

        int _previouslySortedColumn = -1;

        public VizualizareGrafic(string CNP,string limba)
        {
            InitializeComponent();
            Rename(limba);

            this.limba = limba;
            this.CNP = CNP;
            PersistenceManager pm = PersistenceManager.Instance;
            IList<Senzori> listSenzori = pm.RetrieveAll<Senzori>(SessionAction.BeginAndEnd);

            foreach (Senzori s in listSenzori)
            {
                cbListaSenzori.Items.Add(s.NumeSenzor);
                senzorNume.Add(s.IdSenzor, s.NumeSenzor);
            }

            lvDateGrafic.View = View.Details;
            lvDateGrafic.FullRowSelect = true;
            lvDateGrafic.GridLines = false;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(lvDateGrafic.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvDateGrafic.Columns.Add(s);

            pm = PersistenceManager.Instance;
            IList<InvestigatiiSenzor> lista = pm.RetrieveEquals<InvestigatiiSenzor>("IdPacient", CNP);
            lvDateGrafic.Items.Clear();
            populateListView(lista);
        }

        private void VizualizareGrafic_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < lvDateGrafic.Items.Count; i++)
            {
                if (lvDateGrafic.Items[i].Selected == true)
                {

                    ListViewItem itm = lvDateGrafic.SelectedItems[0];

                    int id =0;
                    foreach( int cheie in senzorNume.Keys)
                    {
                        if ( senzorNume[cheie].Equals(itm.SubItems[2].Text))
                        {
                            id = cheie;
                            break;
                        }
                    }

                    if (senzorNume[id].Contains("Temperatura")) 
                    {
                        GraficTemperatura grafic = new GraficTemperatura( DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id,CNP,limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("Lumina"))
                    {
                        GraficLumina grafic = new GraficLumina(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP, limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("ECG"))
                    {
                        GraficECG grafic = new GraficECG(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP,limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("BP"))
                    {
                        GraficBP grafic = new GraficBP(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP,limba);
                        grafic.Show();
                    }
                }

            }
        }

        private void populateListView(IList<InvestigatiiSenzor> list)
        {
            foreach (InvestigatiiSenzor invsenzor in list)
            {
                ListViewItem item = new ListViewItem(invsenzor.DataInceput.ToString());
                item.SubItems.Add(invsenzor.DataSfarsit.ToString());
                item.SubItems.Add(senzorNume[invsenzor.IdSenzor]);
                lvDateGrafic.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvDateGrafic.Columns)
            {
                ch.Width = -2;
            }
        }

        private void cbListaSenzori_SelectedIndexChanged(object sender, EventArgs e)
        {

            string cbNameSelected = cbListaSenzori.SelectedItem.ToString();
            int idSenzor = 0;
            foreach( KeyValuePair<int,string> senzor in senzorNume)
            {
                if (senzor.Value.Equals(cbNameSelected))
                {
                    idSenzor = senzor.Key;
                    break;
                }
            }

            PersistenceManager pm = PersistenceManager.Instance;

            IList<InvestigatiiSenzor> lista = pm.RetrieveEquals<InvestigatiiSenzor>("IdPacient", CNP,"IdSenzor",idSenzor);
            lvDateGrafic.Items.Clear();
            populateListView(lista);
        }

        void listview1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {

            ListViewComparer lvComparer;
            if (null == lvDateGrafic.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvDateGrafic.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumn)
            {
                lvDateGrafic.Sorting = SortOrder.None;
            }
            if (lvDateGrafic.Sorting == SortOrder.Ascending)
            {
                lvDateGrafic.Sorting = SortOrder.Descending;
            }
            else
            {
                lvDateGrafic.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvDateGrafic, ref _previouslySortedColumn, e.Column);
            lvComparer.SortColumn = e.Column;
            lvDateGrafic.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvDateGrafic.Columns)
            {
                ch.Width = -2;
            }
        }


    }
}
